//888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
//
//	File		 :	sequence_gaps.cpp
//	Creation Date:	14/04/2003
//	Last Modified:	14/04/2003	lg
//	tabs width	 :	every 4 characters
//
//888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#include "sequence_gaps.h"
using std::string;
using std::vector;

bool expand_gaps(const t_gap_loci& gaps, string& prot)
{
	for (int i = gaps.size() - 1; i >= 0; --i)
	{
		if (gaps[i].beg > prot.length())
			return false;
		prot.insert(gaps[i].beg, 	 string(gaps[i].len,  	  '-'));
	}
	
	return true;
}
bool expand_gaps_cdna(const t_gap_loci& gaps, string& cdna)
{
	for (int i = gaps.size() - 1; i >= 0; --i)
	{
		if (gaps[i].beg * 3 > cdna.length())
			return false;
		cdna.insert(gaps[i].beg * 3, string(gaps[i].len * 3,  '-'));
	}
	
	return true;
}


void remove_gaps(t_gap_loci& gaps, string& prot)
{
	string new_prot = prot;
	// add terminating null for quicker searches
	prot += '\0';
	string::const_iterator	pos_old	= prot.begin();
	string::const_iterator	pos_end	= prot.end();

	string::iterator		pos_new = new_prot.begin();

	while (*pos_old)
	{
		// get ungapped run
		while (*pos_old && *pos_old != '-')
			*pos_new++ = *pos_old++;

		// break if end
		if (!*pos_old)
			break;

		// gap
		unsigned gap_beg = pos_old - prot.begin();
		while (*pos_old == '-')
			++pos_old;
		unsigned gap_len = pos_old - prot.begin() - gap_beg;
		gaps.push_back(gap_locus(pos_new - new_prot.begin(), gap_len));
	}

	new_prot.erase(pos_new,  new_prot.end());
	prot.swap(new_prot);
}

